const { mealApi, orderApi } = require('../../../utils/api')

Page({
  data: {
    loading: true,
    meal: {
      id: '',
      image: '',
      name: '',
      description: '',
      calories: 0,
      protein: 0,
      carbs: 0,
      fat: 0,
      price: 0,
      ingredients: [],
      steps: [],
      nutritionist: {
        avatar: '',
        name: '',
        review: ''
      }
    }
  },

  onLoad: function(options) {
    if (options.id) {
      this.loadMealDetails(options.id)
    } else {
      // 用于开发测试的模拟数据
      this.setMockData()
    }
  },

  // 加载套餐详情
  loadMealDetails: async function(mealId) {
    try {
      wx.showLoading({
        title: '加载中...'
      })

      const { data } = await mealApi.getMealDetails(mealId)
      
      this.setData({
        meal: data,
        loading: false
      })
    } catch (error) {
      console.error('加载套餐详情失败:', error)
      wx.showToast({
        title: '加载失败，请重试',
        icon: 'none'
      })
    } finally {
      wx.hideLoading()
    }
  },

  // 设置模拟数据（开发测试用）
  setMockData: function() {
    const mockData = {
      id: 'mock-001',
      image: '/images/meals/sample-meal.jpg',
      name: '健康轻食沙拉套餐',
      description: '低脂高蛋白，营养均衡的健康轻食套餐',
      calories: 450,
      protein: 25,
      carbs: 45,
      fat: 15,
      price: 48,
      ingredients: [
        {
          id: 1,
          name: '生菜',
          image: '/images/ingredients/lettuce.jpg',
          amount: '100g',
          nutrition: '富含维生素C'
        },
        {
          id: 2,
          name: '鸡胸肉',
          image: '/images/ingredients/chicken-breast.jpg',
          amount: '150g',
          nutrition: '优质蛋白'
        },
        {
          id: 3,
          name: '藜麦',
          image: '/images/ingredients/quinoa.jpg',
          amount: '50g',
          nutrition: '膳食纤维'
        }
      ],
      steps: [
        {
          title: '食材准备',
          image: '/images/steps/prep.jpg',
          description: '将生菜洗净切碎，鸡胸肉切丁，藜麦煮熟备用',
          tips: '鸡胸肉最好选择新鲜的，避免冷冻品'
        },
        {
          title: '调制酱汁',
          image: '/images/steps/sauce.jpg',
          description: '将橄榄油、柠檬汁、盐和黑胡椒混合均匀',
          tips: '可以根据个人口味调整酱汁酸度'
        },
        {
          title: '组装摆盘',
          image: '/images/steps/plating.jpg',
          description: '将所有食材混合，淋上酱汁即可享用',
          tips: '建议食用前再淋酱汁，保持食材新鲜度'
        }
      ],
      nutritionist: {
        avatar: '/images/nutritionist/avatar.jpg',
        name: '王营养师',
        review: '这是一份营养均衡的健康套餐，特别适合需要控制体重但又不想放弃美味的人群。鸡胸肉提供优质蛋白，藜麦补充膳食纤维和矿物质，新鲜蔬菜则提供必要的维生素。'
      }
    }

    this.setData({
      meal: mockData,
      loading: false
    })
  },

  // 分享功能
  onShareAppMessage: function() {
    const { meal } = this.data
    return {
      title: meal.name,
      path: `/pages/nutrition/meal/index?id=${meal.id}`,
      imageUrl: meal.image
    }
  },

  // 订购功能
  orderMeal: function() {
    const { meal } = this.data
    wx.showModal({
      title: '确认订购',
      content: `是否确认订购"${meal.name}"？`,
      success: (res) => {
        if (res.confirm) {
          this.submitOrder()
        }
      }
    })
  },

  // 提交订单
  submitOrder: async function() {
    try {
      wx.showLoading({
        title: '提交中...'
      })

      const { orderId, orderData } = await orderApi.createOrder(this.data.meal.id)

      wx.showToast({
        title: '订购成功',
        icon: 'success'
      })
      
      // 跳转到订单详情页
      wx.navigateTo({
        url: `/pages/order/detail/index?id=${orderId}`
      })
    } catch (error) {
      console.error('提交订单失败:', error)
      wx.showToast({
        title: '订购失败，请重试',
        icon: 'none'
      })
    } finally {
      wx.hideLoading()
    }
  }
}) 